/*
 * desc：Vuex的封装
 */
import { pmsLogin, getProfile, getMenusUser, token } from '@/api/user.js'
import {
  getToken,
  setToken,
  removeToken,
  getPermission,
  setPermission,
  removePermission,
  getUserName,
  setUserName,
  removeUserName,
  getNickName,
  setNickName,
  removeNickName,
} from './auth.js'
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
import Cookies from 'js-cookie'

const store = new Vuex.Store({
  state: {
    token: getToken(),
    nickname: getNickName() || '',
    username: getUserName() || '',
    permission: getPermission() || [],
    btn_permission: [], // 这东西没必要管刷新那
    //路由
    sidebar: {
      opened: !+Cookies.get('sidebarStatus'),
      withoutAnimation: false,
    },
    device: 'desktop',
  },

  getters: {
    token: (state) => state.token,
    username: (state) => state.username,
    nickname: (state) => state.nickname,
    permission: (state) => state.permission,
    btn_permission: (state) => state.btn_permission,
    //  路由
    sidebar: (state) => state.sidebar,
    device: (state) => state.device,
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_USERNAME: (state, username) => {
      state.username = username
    },
    SET_NICKNAME: (state, nickname) => {
      state.nickname = nickname
    },
    SET_PERMISSION: (state, auth) => {
      state.permission = auth
    },
    SET_BTN_PERMISSION: (state, arr) => {
      state.btn_permission = arr
    },
    TOGGLE_SIDEBAR: (state) => {
      if (state.sidebar.opened) {
        Cookies.set('sidebarStatus', 1)
      } else {
        Cookies.set('sidebarStatus', 0)
      }
      state.sidebar.opened = !state.sidebar.opened
      state.sidebar.withoutAnimation = false
    },
    CLOSE_SIDEBAR: (state, withoutAnimation) => {
      Cookies.set('sidebarStatus', 1)
      state.sidebar.opened = false
      state.sidebar.withoutAnimation = withoutAnimation
    },
    TOGGLE_DEVICE: (state, device) => {
      state.device = device
    },
  },

  actions: {
    // 登陆
    pmsLogin({ commit }, formData) {
      const { username, password } = formData
      return new Promise((resolve, reject) => {
        pmsLogin({ username: username, password: password })
          .then(async (res) => {
            commit('SET_TOKEN', res.data)
            setToken(res.data)
            getProfile({}).then((res) => {
              const { username, nickName } = res.data
              commit('SET_USERNAME', username)
              setUserName(username)
              commit('SET_NICKNAME', nickName)
              setNickName(nickName)
              // 权限
              // commit('SET_PERMISSION', data.auth)
              // setPermission(data.auth);
              commit('SET_PERMISSION', [])
              setPermission([])
              resolve(res.data)
              // token({username:username}).then(e=>{

              // })
            })
            getMenusUser({}).then((res) => {})
          })
          .catch((error) => {
            reject(error)
          })
      })
    },
    // 直接通过username获取token登陆
    pmsTokenLogin({ commit }, username) {
      return new Promise((resolve, reject) => {
        token({ username: username })
          .then(async (res) => {
            commit('SET_TOKEN', res.data)
            setToken(res.data)
            getProfile({}).then((res) => {
              const { username, nickName } = res.data
              commit('SET_USERNAME', username)
              setUserName(username)
              commit('SET_NICKNAME', nickName)
              setNickName(nickName)
              // 权限
              // commit('SET_PERMISSION', data.auth)
              // setPermission(data.auth);
              commit('SET_PERMISSION', [])
              setPermission([])
              resolve(res.data)
              // token({username:username}).then(e=>{

              // })
            })
            getMenusUser({}).then((res) => {})
          })
          .catch((error) => {
            reject(error)
          })
      })
    },
    // 退出
    pmsLogout({ commit }) {
      return new Promise((resolve) => {
        commit('SET_TOKEN', '')
        commit('SET_USERNAME', '')
        commit('SET_NICKNAME', '')
        commit('SET_PERMISSION', '')
        removeToken()
        removeNickName()
        removeUserName()
        removePermission()
        sessionStorage.clear()
        localStorage.clear()
        //刷新避免菜单栏不重新加载
        window.location.reload()
        resolve()
        // pmsLogout().then(res => {
        //   if (res.code === 200) {
        //     commit('SET_TOKEN', '');
        //     commit('SET_USERNAME', '');
        //     commit('SET_NICKNAME', '');
        //     commit('SET_PERMISSION', '');
        //     removeToken();
        //     removeNickName();
        //     removeUserName();
        //     removePermission();
        //     sessionStorage.clear();
        //     localStorage.clear();
        //     //刷新避免菜单栏不重新加载
        //     window.location.reload();
        //     resolve();
        //   }
        //   resolve()
        // }).catch(error => {
        //   reject(error)
        // })
      })
    },
    // 清空
    pmsRemove({ commit }) {
      return new Promise((resolve) => {
        commit('SET_TOKEN', '')
        commit('SET_USERNAME', '')
        commit('SET_NICKNAME', '')
        commit('SET_PERMISSION', '')
        removeToken()
        removeNickName()
        removeUserName()
        removePermission()
        resolve()
      })
    },
    // 按钮权限
    setBtnPermission({ commit }, arr) {
      return new Promise((resolve) => {
        commit('SET_BTN_PERMISSION', arr)
        resolve(arr)
      })
    },
    ToggleSideBar: ({ commit }) => {
      commit('TOGGLE_SIDEBAR')
    },
    CloseSideBar({ commit }, { withoutAnimation }) {
      commit('CLOSE_SIDEBAR', withoutAnimation)
    },
    ToggleDevice({ commit }, device) {
      commit('TOGGLE_DEVICE', device)
    },
  },
})

export default store
